package buffered;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * BufferedWriter类:
 *  构造方法:
 *      public BufferedWriter(Writer out):将基本字符输出流包装成缓冲(缓存)流
 *  成员方法:
 *       public void write(String str) throws IOException:将字符串写入到缓冲(底层字符数组)中
 *       public void newLine() throws IOException:是跨平台方法,不同的操作系统会写入不同的换行符
 *       public void flush() throws IOException:手动刷新,将底层字符数组中的文件刷新到文件中
 *       public void close() throws IOException:先刷新后关流释放资源
 *  BufferedWriter底层原理:
 *      1.在写数据时会先将数据写到BufferedWriter底层开辟的长度为8192的字符数组中(char[] cb = new char[8192])
 *      2.如果写入的字符数超过8192,则会先把8192个字符强制刷新到文件中,然后把当前要写的字符写入底层的8192字符数组中
 *      3.也可以手动调用flush()或close()方法来手动刷新
 */
public class BufferedWriterDemo01 {
    public static void main(String[] args) throws IOException {
        //method01();
        BufferedWriter bw = new BufferedWriter(new FileWriter("day23-IO-02/8.txt"));
        bw.write("abc");
        bw.write("def");
        bw.newLine();//写入一个换行
        //bw.write("\r\n");
        bw.write("你好");
        bw.close();

    }

    private static void method01() throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter("day23-IO-02/7.txt"));
        bw.write("你好");//将"你好"写入BufferedWriter底层的字符数组中
        bw.close();
    }
}
